package github.kawaiior.yuanchuxiuzhen.common.tileentity;

import github.kawaiior.yuanchuxiuzhen.Yuanchuxiuzhen;
import github.kawaiior.yuanchuxiuzhen.common.container.WeaponFurnaceContainer;
import github.kawaiior.yuanchuxiuzhen.common.container.number.WeaponFurnaceContainerItemNumber;
import github.kawaiior.yuanchuxiuzhen.common.item.fire.StrangeFireItem;
import github.kawaiior.yuanchuxiuzhen.common.item.misc.JadeSlipsItem;
import github.kawaiior.yuanchuxiuzhen.common.item.misc.PelletItem;
import github.kawaiior.yuanchuxiuzhen.core.init.TileEntityInit;
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.Inventory;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.INamedContainerProvider;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.tileentity.ITickableTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityType;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;

import javax.annotation.Nullable;

public class WeaponFurnaceTileEntity extends TileEntity implements ITickableTileEntity, INamedContainerProvider {
    private WeaponFurnaceContainerItemNumber itemNumber = new WeaponFurnaceContainerItemNumber();

    private Inventory miscInventory = new Inventory(4);
    private Inventory pelletInventory = new Inventory(1);
    private Inventory fireInventory = new Inventory(1);
    private Inventory weaponInventory = new Inventory(4);
    private Inventory jadeInventory = new Inventory(1);

    private final String miscInventoryString = "misc_inventory_";
    private final String pelletInventoryString = "pellet_inventory";
    private final String fireInventoryString = "fire_inventory";
    private final String weaponInventoryString = "weapon_inventory_";
    private final String jadeInventoryString = "jade_inventory";

    private boolean working = false;
    private int tickTime = 0;

    public WeaponFurnaceTileEntity(TileEntityType<?> tileEntityTypeIn) {
        super(tileEntityTypeIn);
    }

    public WeaponFurnaceTileEntity() {
        super(TileEntityInit.WEAPON_FURNACE_TILE_ENTITY.get());
    }
    @Override
    public ITextComponent getDisplayName() {
        return new TranslationTextComponent("container."+ Yuanchuxiuzhen.MODID+".weapon_furnace_block");
    }

    @Nullable
    @Override
    public Container createMenu(int id, PlayerInventory playerInventory, PlayerEntity player) {
        return new WeaponFurnaceContainer(id, playerInventory, this, itemNumber);
    }

    @Override
    public void tick() {
        tickTime++;
        if (tickTime<20){
            return;
        }
        tickTime = 0;
        if (!working){
            return;
        }
        if (itemNumber.get(0) == itemNumber.getMax()) {
            itemNumber.reset();
            working = false;
            // 产出
            ItemStack jade = jadeInventory.getStackInSlot(0);
            if (!(jade.getItem() instanceof JadeSlipsItem)){
                return;
            }
            JadeSlipsItem jadeItem = ((JadeSlipsItem)jade.getItem());
            Item[] products = jadeItem.getProducts();
            for (int i = 0; i < products.length; i++) {
                weaponInventory.setInventorySlotContents(i, new ItemStack(products[i]));
            }
            // 消耗材料
            int[] itemCount = jadeItem.getItemCount();
            for (int i = 0; i < 4; i++) {
                miscInventory.getStackInSlot(i).shrink(itemCount[i]);
            }
            pelletInventory.getStackInSlot(0).shrink(1);
            fireInventory.getStackInSlot(0).shrink(1);
            // 置空
            jadeInventory.setInventorySlotContents(0, ItemStack.EMPTY);
        }

        Yuanchuxiuzhen.debug("PelletFurnace working tick is "+itemNumber.get(0));
        itemNumber.tick();
    }

    @Override
    public void read(BlockState state, CompoundNBT nbt) {
        for (int i = 0; i < 4; i++) {
            this.miscInventory.setInventorySlotContents(i, ItemStack.read(nbt.getCompound(miscInventoryString+i)));
        }
        this.pelletInventory.setInventorySlotContents(0, ItemStack.read(nbt.getCompound(pelletInventoryString)));
        this.fireInventory.setInventorySlotContents(0, ItemStack.read(nbt.getCompound(fireInventoryString)));
        for (int i = 0; i < 4; i++) {
            this.weaponInventory.setInventorySlotContents(i, ItemStack.read(nbt.getCompound(weaponInventoryString+i)));
        }
        this.jadeInventory.setInventorySlotContents(0, ItemStack.read(nbt.getCompound(jadeInventoryString)));
        super.read(state, nbt);
    }

    @Override
    public CompoundNBT write(CompoundNBT compound) {
        for (int i = 0; i < 4; i++) {
            ItemStack stack = this.miscInventory.getStackInSlot(i).copy();
            compound.put(miscInventoryString+i, stack.serializeNBT());
        }
        compound.put(pelletInventoryString, this.pelletInventory.getStackInSlot(0).copy().serializeNBT());
        compound.put(fireInventoryString, this.fireInventory.getStackInSlot(0).copy().serializeNBT());
        for (int i = 0; i < 4; i++) {
            compound.put(weaponInventoryString+i, this.weaponInventory.getStackInSlot(i).copy().serializeNBT());
        }
        compound.put(jadeInventoryString, this.jadeInventory.getStackInSlot(0).copy().serializeNBT());
        return super.write(compound);
    }

    public WeaponFurnaceContainerItemNumber getItemNumber() {
        return itemNumber;
    }

    public Inventory getMiscInventory() {
        return miscInventory;
    }

    public Inventory getPelletInventory() {
        return pelletInventory;
    }

    public Inventory getFireInventory() {
        return fireInventory;
    }

    public Inventory getWeaponInventory() {
        return weaponInventory;
    }

    public Inventory getJadeInventory() {
        return jadeInventory;
    }

    public void checkWorking(){
        ItemStack jade = jadeInventory.getStackInSlot(0);
        if (jade.isEmpty()){
            return;
        }
        Item item = jade.getItem();
        if (!(item instanceof JadeSlipsItem)){
            return;
        }
        // 检查是否符合
        JadeSlipsItem jadeItem = ((JadeSlipsItem) item);
        Item[] itemArray = jadeItem.getItemArray();
        int[] itemCount = jadeItem.getItemCount();
        for (int i = 0; i < 4; i++) {
            Item ii = itemArray[i];
            ItemStack is = miscInventory.getStackInSlot(i);
            if (ii!=is.getItem()){
                return;
            }
            if (is.getCount()<itemCount[i]){
                return;
            }
        }
        // 检查丹
        ItemStack pelletStack = pelletInventory.getStackInSlot(0);
        if (!(pelletStack.getItem() instanceof PelletItem)){
            return;
        }
        if (((PelletItem)pelletStack.getItem()).getManaRoot()!=jadeItem.getManaRoot()){
            return;
        }
        // 检查火
        ItemStack fireStack = fireInventory.getStackInSlot(0);
        if (!(fireStack.getItem() instanceof StrangeFireItem)){
            return;
        }
        if (((StrangeFireItem)fireStack.getItem()).getLevel() < jadeItem.getFireLevel()){
            return;
        }
        for (int i = 0; i < 4; i++) {
            if (!weaponInventory.getStackInSlot(0).isEmpty()){
                return;
            }
        }
        this.working = true;
        Yuanchuxiuzhen.debug("WeaponFurnace is working !");
    }

    public boolean isWorking() {
        return working;
    }

    public void setWorking(boolean working) {
        this.working = working;
    }
}
